package com.miyake.dsp;

import dk.ange.octave.OctaveEngine;
import dk.ange.octave.OctaveEngineFactory;
import dk.ange.octave.type.OctaveDouble;

public class OctaveExecute {
	private static OctaveEngine octave = null;
	
	public static synchronized ComplexArray execute(String command, ComplexArray data, String result) {
		if (octave == null) {
			octave = new OctaveEngineFactory().getScriptEngine();
		}

		octave.put("data1", new OctaveDouble(data.getRealPart(), 1, data.getRealPart().length));
		octave.eval(command);
		OctaveDouble r = (OctaveDouble)octave.get(result);
//		octave.close();
//		octave.destroy();
//		octave = null;
		
		return new ComplexArray(r.getData(), null);
		
	}
	
	public static synchronized ComplexArray execute(String command, ComplexArray data, ComplexArray data2, String result) {
		if (octave == null) {
			octave = new OctaveEngineFactory().getScriptEngine();
		}

		octave.put("data1", new OctaveDouble(data.getRealPart(), 1, data.getRealPart().length));
		octave.put("data2", new OctaveDouble(data2.getRealPart(), 1, data2.getRealPart().length));
		octave.eval(command);
		OctaveDouble r = (OctaveDouble)octave.get(result);
		return new ComplexArray(r.getData(), null);
		
	}
}
